package cn.rxmind.dlicense.core.validator;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.rxmind.dlicense.core.base.DLicenseContent;
import cn.rxmind.dlicense.core.rule.ActiveTimeRule;
import lombok.Setter;

import java.util.Date;

/**
 * 有效期校验
 *
 * @author renxin
 * @email renxinzhiliangzhi@163.com
 * @date 2021/4/16 3:17 下午
 */
public class LicenseActiveTimeValidator extends AbstractExitJVMLicenseValidator {

    @Override
    public String description() {
        return "证书有效期校验";
    }

    @Override
    protected ValidateResult validateResult(DLicenseContent content) {

        ActiveTimeRule rule = content.getLicenseActiveTimeRule();
        final boolean valid = rule.test(new Date());

        return new ValidateResult().setLicenseValidator(this.getClass()).setValid(valid).setDescription(valid ? "valid" : String.format("证书使用起止时间为：%s,现在是：%s",
                DateUtil.format(rule.getNotBefore(), DatePattern.NORM_DATE_PATTERN),
                DateUtil.format(rule.getNotAfter(), DatePattern.NORM_DATE_PATTERN)));
    }
}
